package com.lzm.ydpt.module.mall.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.baseadapter.rv.RvCommonAdapter;
import com.byt.framlib.baseadapter.rv.RvViewHolder;
import com.byt.framlib.commonutils.DisplayUtil;
import com.byt.framlib.commonutils.ToastUitl;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.FilterBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 取消订单原因
 */
public class CancalOrderDialog implements View.OnClickListener {

    private Dialog mDialog;
    private View dialogView;

    private Context mContext;
    private RecyclerView rv_cancal_reason_list;
    private TextView tv_commit_reason;
    private TextView tv_commit_cancal;

    private List<FilterBean> mFilterList = new ArrayList<>();
    private RvCommonAdapter<FilterBean> beanRvAdapter = null;

    private Builder mBuilder;

    public CancalOrderDialog(Builder builder) {
        this.mBuilder = builder;
        mDialog = new Dialog(mBuilder.getContext(), R.style.MyDialogStyle);
        dialogView = View.inflate(mBuilder.getContext(), R.layout.shop_cancal_order_dialog, null);

        mDialog.setContentView(dialogView); // 一定要在setAttributes(lp)之前才有效
        //设置dialog的宽
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = DisplayUtil.getScreenWidth(mBuilder.getContext());
        lp.height = DisplayUtil.getScreenHeight(mBuilder.getContext());
        dialogWindow.setAttributes(lp);
        mDialog.setCanceledOnTouchOutside(builder.isTouchOutside());

        initView();
    }

    private void setViewData() {
        mFilterList.clear();
        mFilterList.add(new FilterBean("买错了", 1, false));
        mFilterList.add(new FilterBean("不想要了", 2, false));
        mFilterList.add(new FilterBean("地址信息填写错误", 3, false));
        mFilterList.add(new FilterBean("其他", 100, false));
    }

    private void initView() {
        rv_cancal_reason_list = dialogView.findViewById(R.id.rv_cancal_reason_list);
        tv_commit_reason = dialogView.findViewById(R.id.tv_commit_reason);
        setViewData();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        rv_cancal_reason_list.setLayoutManager(layoutManager);
        beanRvAdapter = new RvCommonAdapter<FilterBean>(mContext, mFilterList, R.layout.item_cancal_order_reason) {
            @Override
            protected void convert(RvViewHolder holder, FilterBean filterBean, int position) {
                holder.setSelected(R.id.rl_cancal_reason_info, filterBean.isSelect());
                holder.setSelected(R.id.tv_cancal_reason, filterBean.isSelect());
                holder.setText(R.id.tv_cancal_reason, filterBean.getFilterName());
                holder.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onReset(filterBean.getFilterId());
                        beanRvAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        rv_cancal_reason_list.setAdapter(beanRvAdapter);
        rv_cancal_reason_list.setFocusable(false);
        tv_commit_cancal.setOnClickListener(this);
        tv_commit_reason.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        if (mBuilder == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_commit_cancal:
                dismiss();
                break;
            case R.id.tv_commit_reason:
                dismiss();
                if (mBuilder.getOnListener() != null) {
                    String reason = getSelectResult();
                    if (TextUtils.isEmpty(reason)) {
                        ToastUitl.showShort("请选择取消订单的原因");
                        return;
                    }
                    mBuilder.getOnListener().onReasonResult(reason);
                }
                break;
        }
    }

    public void show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
    }

    public void dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    private String getSelectResult() {
        String reason = "";
        for (FilterBean filterBean : mFilterList) {
            if (filterBean.isSelect()) {
                reason = filterBean.getFilterName();
            }
        }
        return reason;
    }

    private void onReset(int filterId) {
        for (FilterBean filterBean : mFilterList) {
            if (filterBean.getFilterId() == filterId) {
                filterBean.setSelect(!filterBean.isSelect());
            } else {
                filterBean.setSelect(false);
            }
        }
    }

    public interface ReasonResultCall {
        void onReasonResult(String reason); // 取消订单原因
    }

    public static class Builder {

        private ReasonResultCall onListener;
        private boolean isTouchOutside;
        private Activity mContext;

        public Builder(Activity context) {
            this.mContext = context;
            this.onListener = null;
            this.isTouchOutside = true;
        }

        public ReasonResultCall getOnListener() {
            return onListener;
        }

        public Builder setOnListener(ReasonResultCall onListener) {
            this.onListener = onListener;
            return this;
        }

        public boolean isTouchOutside() {
            return isTouchOutside;
        }

        public Builder setCanceledOnTouchOutside(boolean isTouchOutside) {
            this.isTouchOutside = isTouchOutside;
            return this;
        }

        public CancalOrderDialog build() {
            return new CancalOrderDialog(this);
        }

        public Activity getContext() {
            return mContext;
        }

        public void setContext(Activity context) {
            mContext = context;
        }

        public void setTouchOutside(boolean touchOutside) {
            isTouchOutside = touchOutside;
        }
    }
}